package com.yidusoft.controller;

import com.yidusoft.core.Result;
import com.yidusoft.core.ResultGenerator;
import com.yidusoft.domain.SecMenu;
import com.yidusoft.domain.SecMenuMember;
import com.yidusoft.domain.SecMenuRole;
import com.yidusoft.domain.SecRole;
import com.yidusoft.service.SecMenuMemberService;
import com.yidusoft.service.SecMenuRoleService;
import com.yidusoft.service.SecMenuService;
import com.yidusoft.service.SecRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidusoft.utils.CodeHelper;
import com.yidusoft.utils.TreeNode;
import org.springframework.stereotype.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by CodeGenerator on 2017/07/18.
 */
@Controller
@RequestMapping("/sec/role")
public class SecRoleController {
    @Resource
    private SecRoleService secRoleService;

    @RequestMapping(value ="/linkAdd")
    public String linkAdd(){
        return  "secrole/secrole-add";
    }

    @RequestMapping(value ="/linkList")
    public String linkList(){
        return  "secrole/secrole-list";
    }

    @RequestMapping(value ="/linkUpdate")
    public String linkUpdate(Model model,String id){
        SecRole secRole = secRoleService.findById(id);
        model.addAttribute("secRole",secRole);
        return  "secrole/secrole-update";
    }

    @PostMapping("/add")
    @ResponseBody
    public Result add(SecRole secRole) {
        secRole.setRoleCode(CodeHelper.getCode("R"));
        secRole.setId(UUID.randomUUID().toString());
        secRole.setCreateTime(new Date());
        secRoleService.save(secRole);
        return ResultGenerator.genSuccessResult();
    }

    //把数据永久删除
    @PostMapping("/deleteTrue")
    @ResponseBody
    public Result deleteTrue(String  id) {
        secRoleService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    //逻辑删除，数据还在
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(String  id) {
        SecRole secRole = secRoleService.findById(id);
        secRole.setDeleted(1);
        secRoleService.update(secRole);
        return ResultGenerator.genSuccessResult();
    }


    //逻辑删除，数据还在 批量删除
    @PostMapping("/deleteBacth")
    @ResponseBody
    public Result deleteBacth(String  ids) {
        String arr [] = ids.split(",");
        for(String str : arr){
            SecRole secRole = secRoleService.findById(str);
            secRole.setDeleted(1);
            secRoleService.update(secRole);
        }
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(SecRole secRole) {
        secRoleService.update(secRole);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ResponseBody
    public Result detail(@PathVariable String id) {
        SecRole secRole = secRoleService.findById(id);
        return ResultGenerator.genSuccessResult(secRole);
    }
    @Resource
    private SecMenuMemberService secMenuMemberService;
    @Resource
    private  SecMenuRoleService secMenuRoleService;
    @Resource
    private SecMenuService secMenuService;
    @PostMapping("/roleAndMemberList")
    @ResponseBody
    public  List<TreeNode> roleAndMemberList(String id) {
        List<TreeNode> Nodes =secMenuService.menuTree();
        List<SecMenuMember> secMenuMembers = secMenuMemberService.findUserMenu(id);
        List<SecMenuRole> secMenuRoles = secMenuRoleService.findRoleMenu(id);
        for(SecMenuRole secMenuRole : secMenuRoles){
            for(TreeNode treeNode : Nodes){
                if(secMenuRole.getMenuId().equals(treeNode.getId())){
                    treeNode.setFlag("0");//角色权限
                }
            }
        }
        for(SecMenuMember secMenuMember : secMenuMembers){
            for(TreeNode treeNode : Nodes){
                if(secMenuMember.getMenuId().equals(treeNode.getId())){
                    treeNode.setFlag("1");//标识有权限
                }
            }
        }
        return Nodes;
    }

    @PostMapping("/roleMenuhList")
    @ResponseBody
    public Map<String,Object> roleMenuhList(String id) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<SecMenuRole> secMenuRoles = secMenuRoleService.findRoleMenu(id);
        map.put("roles",secMenuRoles);
        return map;
    }

    @PostMapping("/list")
    @ResponseBody
    public Map<String,Object> list(SecRole secRole) {
        Map<String,Object> map = new HashMap<String,Object>();
        List<SecRole> list = new ArrayList<>();
        if(secRole!=null){
            list = secRoleService.getAll(secRole);
        }else {
           list = secRoleService.getRole();
        }
        map.put("Rows",list);
        map.put("Total",list.size());
        return map;
    }


    @PostMapping("/listByPage")
    @ResponseBody
    public Result listByPage(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SecRole> list = secRoleService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
